$(function () {
    var abandonStudent_datagrid = $("#abandonStudent_datagrid");
    var abandonStudent_dialog = $("#abandonStudent_dialog");
    var abandonStudent_form = $("#abandonStudent_form");
    var pay_dialog = $("#pay_dialog");
    var pay_form = $("#pay_form");
    abandonStudent_datagrid.datagrid({
        url: '/abandonStudent/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {
                field: 'sn', title: '学员姓名', width: 100, formatter: function (value, row) {
                return row.formalStudent.name;
            }
            },
            {
                field: 'tel', title: '联系电话', width: 100, formatter: function (value, row) {
                return row.formalStudent.tel;
            }
            },
            {
                field: 'class', title: '班级', width: 100, formatter: function (value, row) {
                return row.formalStudent.intentionClass.className;
            }
            },
            {
                field: 'phase', title: '流失阶段', width: 100, formatter: function (value) {
                return value ? value.name : '';
            }
            },
            {field: 'cause', title: '流失原因', width: 100,},
            {field: 'abandonTime', title: '流失时间', width: 100,},
            {field: 'numberDay', title: '上课天数', width: 100,},
            {
                field: 'refund', title: '是否退款', width: 100, formatter: function (value) {
                return value ? '<font color="green">是</font>' : '<font color="red">否</font>'
            }
            },
            {
                field: 'salemen', title: '营销人员', width: 100, formatter: function (value, row) {
                return row.formalStudent.salemen.username;
            }
            },
            {
                field: 'operator', title: '经办人', width: 100, formatter: function (value) {
                return value.username;
            }
            },
            {
                field: 'status', title: '审核状态', width: 100, formatter: function (value) {
                return value ? '<font color="green">已审核</font>' : '<font color="red">未审核</font>'
            }
            },
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#abandonStudent_btns",
        sortName: 'hireDate',
        sortOrder: 'desc'
    });
    abandonStudent_dialog.dialog({
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            abandonStudent_form.form('clear');
        }
    });
    pay_dialog.dialog({
        title:'退款明细',
        buttons: "#audit_btns",
        closed: true,
        onClose: function () {
            abandonStudent_form.form('clear');
        }
    });
    var methodObj = {
        audit: function () {
            var row = abandonStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定审核该条数据吗？', function (r) {
                if (r) {
                    pay_form.form('load',row);
                    pay_dialog.dialog('open');
                }
            });
        },
        save_audit: function () {
            var row = abandonStudent_datagrid.datagrid("getSelected");
            pay_form.form('submit', {
                url: '/abandonStudent/audit.do',
                onSubmit:function (param) {
                    param['abandonStudentId'] = row.id;
                },
                success: function (data) {
                    $.parseJSON(data);
                }
            })
        },
        cancel_audit: function () {
            pay_dialog.dialog('close');
        },
        watch: function () {
            var row = abandonStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            abandonStudent_form.form('load', row
            );
            abandonStudent_dialog.dialog('setTitle', '编辑部门');
            abandonStudent_dialog.dialog('open');
        },
        changeState: function () {
            var row = abandonStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/abandonStudent/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            abandonStudent_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        del: function () {
            var row = abandonStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该部门吗？', function (r) {
                if (r) {
                    $.get("/abandonStudent/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            abandonStudent_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            abandonStudent_datagrid.datagrid('reload');
        },

        save: function () {
            abandonStudent_form.form('submit', {
                url: '/abandonStudent/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObj.cancel();
                        abandonStudent_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            abandonStudent_dialog.dialog('close');
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })
})



